package com.yilu.driver.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.widget.Toast;

import com.is.ui.base.BaseAppCompatActivity;
import com.is.ui.eventbus.EventCenter;
import com.is.ui.netstatus.NetUtils;
import com.is.ui.selectrecyclerview.DragSelectRecyclerView;
import com.is.ui.selectrecyclerview.DragSelectRecyclerViewAdapter;
import com.yilu.driver.R;
import com.yilu.driver.ui.adapter.SelectRecyclerViewAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectRecyclerViewActivity extends BaseAppCompatActivity implements
        SelectRecyclerViewAdapter.ClickListener, DragSelectRecyclerViewAdapter.SelectionListener {

    @Bind(R.id.list)
    DragSelectRecyclerView list;
    @Bind(R.id.list2)
    DragSelectRecyclerView list2;
    private SelectRecyclerViewAdapter mAdapter;
    private boolean flag = false;

    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void showException(String msg) {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_select_recycler_view;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected void initViewsAndEvents() {
        mAdapter = new SelectRecyclerViewAdapter(this);
        mAdapter.setSelectionListener(this);
        list.setLayoutManager(new GridLayoutManager(this, 3));
        list.setAdapter(mAdapter);
        list2.setLayoutManager(new GridLayoutManager(this, 3));
        list2.setAdapter(mAdapter);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected boolean isApplySystemBarColor() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onDragSelectionChanged(int count) {

    }

    @Override
    public void onClick(int index) {
        mAdapter.toggleSelected(index);
    }

    @Override
    public void onLongClick(int index) {
        list.setDragSelectActive(true, index);
    }

    @OnClick(R.id.button)
    public void onClick() {
        flag = !flag;
        if (flag) {
            mAdapter.selectAll();
            StringBuilder sb = new StringBuilder();
            int traverse = 0;
            for (Integer index : mAdapter.getSelectedIndices()) {
                if (traverse > 0) sb.append(", ");
                sb.append(mAdapter.getItem(index));
                traverse++;
            }
            Toast.makeText(this,
                    String.format("Selected letters (%d): %s", mAdapter.getSelectedCount(), sb.toString()),
                    Toast.LENGTH_LONG).show();
        } else
            mAdapter.clearSelected();
    }
}
